package testimage.zhujj.com.gesturedemo;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ArgbEvaluator;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.ShapeDrawable;
import android.os.Build;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
import android.view.animation.DecelerateInterpolator;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * Created by zhujj.
 */
public class ChangeRankView extends FrameLayout {


    private Context mCtx;
    private RelativeLayout mLiveView;
    private ImageView mLiveAvater;
    private TextView mLiveRank;
    private TextView mLiveRankName;
    private RelativeLayout mBeRankLiveView;
    private ImageView mBeRankLiveAvater;
    private TextView mBeRankLiveRank;
    private TextView mBeRankLiveRankName;
    private TextView mRankText;
    private GradientDrawable background;
    private int moveDis;
    private int distance;
    private ImageView mBg;

    public ChangeRankView(Context context) {
        super(context);
        inits(context);
    }

    public ChangeRankView(Context context, AttributeSet attrs) {
        super(context, attrs);
        inits(context);
    }

    public ChangeRankView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        inits(context);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public ChangeRankView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        inits(context);
    }

    private void inits(Context context) {
        this.mCtx = context;
        moveDis = getPixels(55);
        distance = getPixels(200);
        View v = inflate(context, R.layout.hani_view_change_rank, this);
        mBg = (ImageView) v.findViewById(R.id.hani_rankchange_bg);
        mRankText = (TextView) v.findViewById(R.id.hani_rankchange_text);

        mLiveView = (RelativeLayout) v.findViewById(R.id.hani_changerank_live_ll);
        mLiveAvater = (ImageView) v.findViewById(R.id.hani_changerank_live_avater);
        mLiveRank = (TextView) v.findViewById(R.id.hani_changerank_live_rank);
        mLiveRankName = (TextView) v.findViewById(R.id.hani_changerank_live_rank_name);
        background = (GradientDrawable) mLiveView.getBackground();

        mBeRankLiveView = (RelativeLayout) v.findViewById(R.id.hani_changerank_live1_ll);
        mBeRankLiveAvater = (ImageView) v.findViewById(R.id.hani_changerank_live1_avater);
        mBeRankLiveRank = (TextView) v.findViewById(R.id.hani_changerank_live1_rank);
        mBeRankLiveRankName = (TextView) v.findViewById(R.id.hani_changerank_live1_rank_name);
    }

    public void setData() {
    }

    public void startAnimation() {
        mRankText.clearAnimation();
        mLiveView.clearAnimation();
        mBeRankLiveView.clearAnimation();
        mBg.clearAnimation();
        // TODO 下降的动画
        ObjectAnimator objectAnimator0 = ObjectAnimator.ofFloat(mRankText, View.TRANSLATION_Y, -distance * 3, 0);
        objectAnimator0.setInterpolator(new DecelerateInterpolator());
        ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(mBeRankLiveView, View.TRANSLATION_Y, -distance * 2, 0);
//        ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(mBeRankLiveView,"translationY", -distance * 2, 0);
        objectAnimator1.setInterpolator(new DecelerateInterpolator());
        ObjectAnimator objectAnimator2 = ObjectAnimator.ofFloat(mLiveView, View.TRANSLATION_Y, -distance, 0);
        objectAnimator2.setInterpolator(new DecelerateInterpolator());
        ObjectAnimator objectAnimator3 = ObjectAnimator.ofFloat(mBg, View.TRANSLATION_Y, -distance, 0);
        objectAnimator3.setInterpolator(new DecelerateInterpolator());
        AnimatorSet set = new AnimatorSet();
        set.setDuration(400);
        set.playTogether(objectAnimator0, objectAnimator1, objectAnimator2, objectAnimator3);

        // TODO 换位置动画
        ObjectAnimator objectAnimator4 = ObjectAnimator.ofFloat(mBeRankLiveView, View.TRANSLATION_Y, 0, moveDis);
        ObjectAnimator objectAnimator5 = ObjectAnimator.ofFloat(mLiveView, View.TRANSLATION_Y, 0, -moveDis);
        ObjectAnimator objectAnimator6 = ObjectAnimator.ofFloat(mLiveView, View.SCALE_X, 1, 1.8f, 1);
        ObjectAnimator objectAnimator7 = ObjectAnimator.ofFloat(mLiveView, View.SCALE_Y, 1, 1.8f, 1);

        final AnimatorSet set1 = new AnimatorSet();

        // TODO 颜色平滑过去动画
        ValueAnimator animator = ValueAnimator.ofInt(0x4c000000, 0xffff2d55);
        animator.setEvaluator(new ArgbEvaluator());
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int curValue = (int) animation.getAnimatedValue();
                background.setColor(curValue);

            }
        });
        set.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {
                mRankText.setVisibility(VISIBLE);
                mLiveView.setVisibility(VISIBLE);
                mBeRankLiveView.setVisibility(VISIBLE);
                mBg.setVisibility(VISIBLE);
            }

            @Override
            public void onAnimationEnd(Animator animator) {
                mLiveRank.setTextColor(getResources().getColor(R.color.hani_anchor_control_recommand));
                set1.start();
            }

            @Override
            public void onAnimationCancel(Animator animator) {

            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });
        set1.setDuration(400);
        set1.playTogether(objectAnimator4, objectAnimator5, objectAnimator6, objectAnimator7, animator);
        set1.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {

            }

            @Override
            public void onAnimationEnd(Animator animator) {
                leaveAnimation();
            }

            @Override
            public void onAnimationCancel(Animator animator) {

            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });
        set.start();
    }

    public void leaveAnimation() {
        mRankText.clearAnimation();
        mLiveView.clearAnimation();
        mBeRankLiveView.clearAnimation();

        ObjectAnimator objectAnimator0 = ObjectAnimator.ofFloat(mRankText, View.TRANSLATION_Y, 0, -distance);
        objectAnimator0.setInterpolator(new DecelerateInterpolator());
//        ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(mBeRankLiveView, View.TRANSLATION_Y, moveDis, -distance);
//        objectAnimator1.setInterpolator(new DecelerateInterpolator());
//        objectAnimator1.setStartDelay(200);
//        ObjectAnimator objectAnimator2 = ObjectAnimator.ofFloat(mLiveView, View.TRANSLATION_Y, -moveDis, -distance - 2 * moveDis);
//        objectAnimator2.setInterpolator(new DecelerateInterpolator());
//        objectAnimator2.setStartDelay(100);
        ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(mBeRankLiveView, View.TRANSLATION_Y, moveDis, -distance);
        objectAnimator1.setInterpolator(new DecelerateInterpolator());
        objectAnimator1.setStartDelay(200);
        ObjectAnimator objectAnimator2 = ObjectAnimator.ofFloat(mLiveView, View.TRANSLATION_Y, -moveDis, -distance - 2 * moveDis);
        objectAnimator2.setInterpolator(new DecelerateInterpolator());
        objectAnimator2.setStartDelay(100);
        ObjectAnimator objectAnimator3 = ObjectAnimator.ofFloat(mBg, View.TRANSLATION_Y, 0, -distance);
        objectAnimator3.setInterpolator(new DecelerateInterpolator());
        AnimatorSet set = new AnimatorSet();
        set.setStartDelay(400);
        set.setDuration(400);
        set.playTogether(objectAnimator0, objectAnimator1, objectAnimator2, objectAnimator3);
        set.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {

            }

            @Override
            public void onAnimationEnd(Animator animator) {
                mRankText.setVisibility(INVISIBLE);
                mLiveView.setVisibility(INVISIBLE);
                mBeRankLiveView.setVisibility(INVISIBLE);
                mBg.setVisibility(INVISIBLE);
                background.setColor(getResources().getColor(R.color.hani_c02with30alpha));
                mLiveRank.setTextColor(getResources().getColor(R.color.hani_changerank_rank_color));
            }

            @Override
            public void onAnimationCancel(Animator animator) {

            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });
        set.start();

    }

    /**
     * 出现的动画
     */
    public void startDownAnimation() {
        mBeRankLiveRank.setTextColor(getResources().getColor(R.color.hani_anchor_control_recommand));
        mRankText.clearAnimation();
        mLiveView.clearAnimation();
        mBeRankLiveView.clearAnimation();
        mBg.clearAnimation();
        // TODO 移动动画
        ObjectAnimator objectAnimator0 = ObjectAnimator.ofFloat(mRankText, View.TRANSLATION_Y, -distance * 2, 0);
        objectAnimator0.setInterpolator(new DecelerateInterpolator());
        ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(mBeRankLiveView, View.TRANSLATION_Y, -distance, 0);
        objectAnimator1.setInterpolator(new DecelerateInterpolator());
        ObjectAnimator objectAnimator3 = ObjectAnimator.ofFloat(mBg, View.TRANSLATION_Y, -distance, 0);
        objectAnimator3.setInterpolator(new DecelerateInterpolator());
        final AnimatorSet set = new AnimatorSet();
        set.setDuration(1300);
        set.playTogether(objectAnimator0, objectAnimator1, objectAnimator3);


        ObjectAnimator objectAnimator4 = ObjectAnimator.ofFloat(mBeRankLiveRank, View.ALPHA, 1, 0.2f);
        ObjectAnimator objectAnimator5 = ObjectAnimator.ofFloat(mBeRankLiveRank, View.ALPHA, 0.2f, 1);
        objectAnimator4.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                mBeRankLiveRank.setTextColor(getResources().getColor(R.color.hani_changerank_rank_color));
                mBeRankLiveRank.setText("哈哈");
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });

        final AnimatorSet set1 = new AnimatorSet();
        set.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {
                mRankText.setVisibility(VISIBLE);
                mBeRankLiveView.setVisibility(VISIBLE);
                mBg.setVisibility(VISIBLE);
            }

            @Override
            public void onAnimationEnd(Animator animator) {
                set1.start();
            }

            @Override
            public void onAnimationCancel(Animator animator) {

            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });
        set1.setDuration(300);
        set1.playSequentially(objectAnimator4, objectAnimator5);
        set1.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                leaveDownAnimation();
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        set.start();
    }

    /**
     * 返回动画
     */
    public void leaveDownAnimation() {
        mRankText.clearAnimation();
        mLiveView.clearAnimation();
        mBeRankLiveView.clearAnimation();
        mBg.clearAnimation();

        ObjectAnimator objectAnimator0 = ObjectAnimator.ofFloat(mRankText, View.TRANSLATION_Y, 0, -distance);
        objectAnimator0.setInterpolator(new DecelerateInterpolator());
        ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(mBeRankLiveView, View.TRANSLATION_Y, 0, -distance);
        objectAnimator1.setInterpolator(new DecelerateInterpolator());
        objectAnimator1.setStartDelay(100);
        ObjectAnimator objectAnimator3 = ObjectAnimator.ofFloat(mBg, View.TRANSLATION_Y, 0, -distance);
        objectAnimator3.setInterpolator(new DecelerateInterpolator());
        AnimatorSet set = new AnimatorSet();
        set.setDuration(1300);
        set.playTogether(objectAnimator0, objectAnimator1, objectAnimator3);
        set.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {

            }

            @Override
            public void onAnimationEnd(Animator animator) {
                mRankText.setVisibility(INVISIBLE);
                mLiveView.setVisibility(INVISIBLE);
                mBeRankLiveView.setVisibility(INVISIBLE);
                mBg.setVisibility(INVISIBLE);
                background.setColor(getResources().getColor(R.color.hani_c02with30alpha));
                mLiveRank.setTextColor(getResources().getColor(R.color.hani_changerank_rank_color));
            }

            @Override
            public void onAnimationCancel(Animator animator) {

            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });
        set.start();

    }

    /**
     * 转变为标准尺寸 :dp
     * @param dip
     * @return
     */
    public int getPixels(float dip) {
        return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, getDisplayMetrics()));
    }

    /**
     * 获取手机屏幕参数
     * @return
     */
    public DisplayMetrics getDisplayMetrics() {
        return mCtx.getResources().getDisplayMetrics();
    }
}
